﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Reflection;

namespace WebDataControls
{
    [DefaultProperty("DataSourceID")]
    [ToolboxData("<{0}:DataDropDownList runat=server></{0}:DataDropDownList>")]
    public class DataDropDownList : System.Web.UI.WebControls.DropDownList, IDataControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string FeildName
        {
            get
            {
                String s = (String)ViewState["FeildName"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["FeildName"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TableName
        {
            get
            {
                String s = (String)ViewState["TableName"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["TableName"] = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
        }


        #region IDataControl<T> 成员


        public void GetDataValue(ref object model)
        {
            
            string[] strs = this.FeildName.Split(',');

            PropertyInfo prop = model.GetType().GetProperty(strs[0], BindingFlags.Public | BindingFlags.Instance);
            if (null != prop && prop.CanWrite)
            {
                object value = this.SelectedValue;
                Type fieldType = WebUIHelper.GetModelFieldType(model, strs[0]);
                if (!this.SelectedValue.GetType().Equals(fieldType))
                {
                    if ((String.IsNullOrEmpty(this.SelectedValue)) || this.SelectedValue.Contains("请选择"))
                        return;
                    value = Convert.ChangeType(this.SelectedValue, fieldType);
                }
                prop.SetValue(model, value, null);
            }

            if (strs.Length > 1)
            {
                prop = model.GetType().GetProperty(strs[1], BindingFlags.Public | BindingFlags.Instance);
                if (null != prop && prop.CanWrite)
                {
                    object value = this.SelectedItem.Text;
                    Type fieldType = WebUIHelper.GetModelFieldType(model, strs[1]);
                    if (this.SelectedItem.Text.Contains("请选择"))
                    {
                        value = string.Empty;
                    }
                    if (!this.SelectedItem.Text.GetType().Equals(fieldType))
                    {
                        if (String.IsNullOrEmpty(this.SelectedValue)) 
                            return;
                        value = Convert.ChangeType(this.SelectedItem.Text, fieldType);
                    }
                    prop.SetValue(model, value, null);
                }
            }
        }

        public void SetDataValue(object model)
        {
            if (!String.IsNullOrEmpty(this.FeildName))
            {
                object value = model.GetType().GetProperty(this.FeildName.Split(',')[0], BindingFlags.Public | BindingFlags.Instance).GetValue(model, null);
                if (value != null)
                {
                    this.SelectedValue = value.ToString();
                }
            }
        }

        #endregion
    }
}
